package smart.rpc.server;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created By gao_e on 2020/3/7 23:18
 */
public class SmartRPCServerApplication {
    private static int port;
    private static String registHost;
    public static void start(String[] args) {
        // 获取 启动参数
        if (args != null && args.length > 0) {
            for (String arg : args) {
                String[] split = arg.split("=");
                if (arg.startsWith("port"))
                    port = Integer.valueOf(split[1]);
                if (arg.startsWith("registHost"))
                    registHost = split[1];
            }
        }
        if (port == 0) {
            // TODO 加载传统非springboot项目的默认配置文件读取配置 比如 : smart-rpc.properties
        }
        if (port == 0)
            throw new RuntimeException("Server port not specified");

        System.out.println("准备使用netty启动RPC-Server,服务端口: " + port);

        // 使用netty 异步启动服务
        new StartNettyServerTask(port).useInnerPrivateThreadPoolExecuteSelf();

        // 写出服务信息到文件(注册中心)
        addServiceInfoToFile();

    }

    private static void addServiceInfoToFile() {
        // 注册服务到 文件
        File file = new File(registHost);
        PrintWriter pw = null;
        Set<String> serviceClassFullNames = ServerServiceContainer.getServiceClassFullNames();

        String hostAddress = "127.0.0.1";
        try {
            InetAddress ip4 = Inet4Address.getLocalHost();
            hostAddress = ip4.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        Map<String, String[]> map = new HashMap<>(512);
        for (String serviceClassFullName : serviceClassFullNames) {
            Object bean = ServerServiceContainer.getBeanByServiceFullBame(serviceClassFullName);
            Class<?>[] interfaces = bean.getClass().getInterfaces();
            for (Class<?> inf : interfaces) {
                String[] strs = new String[3];
                strs[0] = hostAddress;
                strs[1] = Integer.toString(port);
                strs[2] = serviceClassFullName;
                map.put(inf.getName(), strs);
            }
        }
        String mapStr = JSONObject.toJSONString(map);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            pw = new PrintWriter(fileOutputStream, true);
            pw.println(mapStr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                if (pw != null) {
                    pw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

